/**
 * @author Axel Huizinga - axel@cunity.me
 * All rights reserved
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package me.cunity.service;
import haxe.Md5;
import haxe.Timer;
import me.cunity.php.dom.DOMDocument;
import php.FileSystem;
import php.Session;
import php.Web;
//import mtwin.mail.Exception;

class User 
{
	
	public static var userList:Array<String>;
	
	public var authenticated(default, null):Bool;
	public var name:String;
	
	var password:String;
	var response:Response;
	var service:Service;
	
	public function new(s:Service)
	{
		//authenticated = false;
		service = s;
		//trace(online);
		//response = 'Welcome - currently online:' + online;
	}
	

	
	public function login(name:String, password:String):Response
	{
		response = new Response();
		untyped __call__('error_log', 'method:' + response.method);
		return response;
	}
	
	public function register(name:String, email:String, password:String):Response 
	{
		response = new Response();
		if(!Util.isValidName(name))
		{
			response.hasError = true;
			response.errorContent += Templater.getSystemTemplate(service, ['errUserName']) + '<br/>';
			
		}
		if (!Util.isValidEmail(email))
		{
			response.hasError = true;
			response.errorContent += Templater.getSystemTemplate(service, ['errEmailInvalid']) + '<br/>';
		}
		
		if (response.hasError)
		{
			return response;			
		}
		var userExists:Bool = FileSystem.exists(service.dataDir + '/users/' + email + '/' + name );
		service.errLog( 'userExists:' + Std.string(userExists) + ':' + service.dataDir + '/users/' + email + '/' + name);
		if (!userExists) {
			if (! FileSystem.exists(service.dataDir + '/users/' + email ))
				if (!FileSystem.createDirectory(service.dataDir + '/users/' + email )) {
					response.hasError = true;
					//response.hasError = "Couldn't create directory:" + service.dataDir + '/users/' + email;
					return response;
				}
			var userData:DOMDocument = new DOMDocument();
			userData.loadXML('<?xml version="1.0" encoding="utf-8" ?><user />');
			userData.documentElement.setAttribute('created', Date.now().toString()); 
			var password:String = Md5.encode(Web.getClientIP());
			userData.documentElement.setAttribute('password', Md5.encode(password));
			if ( userData.save(service.dataDir + '/users/' + email + '/' + name) == 0) {
				response.hasError = true;
				//response.hasError = "Couldn't save user data :-(";
				return response;
			}
			
		}		
		return response;		
	}
	
	public function resetPassword(email:String):Response 
	{
		response = new Response();
		
		/*response.content.set('body', "Welcome " + name + "\n An email to reset your password has been sent to " + email + 
			".\nIt is recommendet to change it immediately after your first Login");*/
		var mail:Mail = new Mail('service@cunity.me', email, 'Registration');
		mail.addContent('text/html', Templater.process(service, 'user/recoverPassword.xml', 
			{
				name:name,
				password:password
			}
		));
		mail.send();
		return response;
	}
	
}